package com.qkst.website.web.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.qkst.website.common.utils.StringUtils;
import com.qkst.website.web.bean.BargainDetailBean;
import com.qkst.website.web.bean.BargainLogBean;
import com.qkst.website.web.mapper.BargainDetailMapper;
import com.qkst.website.web.mapper.BargainLogMapper;
import com.qkst.website.web.service.BargainDetailService;

/**
 * @author lihao
 * @description 砍价明细service
 * @date 2017-05-09
 * @email ahlxlihao@163.com
 *
 */
@Service("bargainDetailService")
@Transactional
public class BargainDetailServiceImpl implements BargainDetailService {
	@Autowired
	private BargainLogMapper bargainMapper;
	@Autowired
	private BargainDetailMapper bargainDetailMapper;
	
	/**
	 * 保存砍价明细
	 * @param bargainLog
	 * @return boolean
	 * @throws Exception 
	 */
	@Override
	public void save(BargainDetailBean bargainDetail,Map<String, Object> result) throws Exception {
		//检查是token是否有效
		List<BargainLogBean> bargainLogList=bargainMapper.selectBargainLogByCondition(bargainDetail.getBargainLog());
		if(bargainLogList!=null && bargainLogList.size()==1 && bargainLogList.get(0).getBargainId()!=null){//token有效
			boolean validate=false;
			BargainLogBean bargainLog=bargainLogList.get(0);
			BigDecimal amount=BigDecimal.ZERO;
			BigDecimal min =BigDecimal.ZERO;
			BigDecimal offerPrice = bargainLog.getProduct().getOfferPrice().getPrice()==null?BigDecimal.ZERO:bargainLog.getProduct().getOfferPrice().getPrice();//产品价格
			BigDecimal max=offerPrice.subtract(bargainLog.getAmount());
			if(bargainLog.getBuyerId().equals(bargainDetail.getUserId())){//如果是自己给自己砍价
				result.put("isSuccess", false);//砍价失败
				result.put("code","777");//自己给自己砍价
				result.put("msg","对不起,您不能自己给自己砍价噢。需要邀请好友才能砍价噢.");
			}else{
				if(bargainLog.getBargainDetailList()!=null && !bargainLog.getBargainDetailList().isEmpty()){//有过砍价明细
					boolean flag=false;//该账号是否砍过价
					//判断当前账号是否参与过此次砍价活动
					for(BargainDetailBean detail:bargainLog.getBargainDetailList()){
						if(detail.getUserId().equals(bargainDetail.getUserId())){
							flag=true;
							break;
						}
					}
					if(!flag){//如果该用户没参与过此次砍价  保存本次砍价明细 并告知客户砍价成功和本次参加砍价帮助砍掉多少钱
						if(bargainLog.getTimes()==4){//如果是当前是第五次 砍完价格
							amount=max.subtract(bargainLog.getAmount());
						}else if(bargainLog.getTimes()>0 &&bargainLog.getTimes()<4){//如果已砍价次数少于4次
							amount=new BigDecimal(StringUtils.round(StringUtils.nextDouble(min.doubleValue() - 1, max.doubleValue() + 1), 2));
						}
						validate=true;
					}else{//如果参与过提示一个账号不能砍多次
						result.put("isSuccess", false);//砍价失败
						result.put("code","222");//重复砍价
						result.put("msg","对不起,您已经成功砍价过一次,一个人只能砍价一次噢。");
					}
					
				}else{//没有砍价明细  直接保存本次砍价明细  首次砍价
						max=max.divide(new BigDecimal(2));//除以2
						amount=new BigDecimal(StringUtils.round(StringUtils.nextDouble(min.doubleValue() - 1, max.doubleValue() + 1), 2));
						validate=true;
				}
				
				if(validate){//验证通过,保存砍价信息
					BargainLogBean upBargainLog=new BargainLogBean();
					upBargainLog.setAmount(bargainLog.getAmount().add(amount));//设置砍价记录总金额
					upBargainLog.setTimes(bargainLog.getTimes()+1);//设置砍价次数+1
					upBargainLog.setLastUpdate(new Date());//设置更新时间
					upBargainLog.setBargainId(bargainLog.getBargainId());
					Short status=1;//砍价完成
					if(upBargainLog.getTimes()>=5){//大于等于5次 设置砍价完成
						upBargainLog.setStatus(status);
					}
					bargainDetail.setAmount(amount);//设置本次砍价金额
					bargainDetail.setBargainTime(new Date());//设置砍价时间
					bargainDetail.setBargainId(bargainLog.getBargainId());
					bargainDetail.setStatus(status);
					int num=bargainDetailMapper.insert(bargainDetail);
					if(num>0){//如果插入成功
						num=bargainMapper.updateByPrimaryKeySelective(upBargainLog);
						if(num>0){
							result.put("isSuccess", true);//砍价成功
							result.put("code","000");
							result.put("bargainLog",bargainLog);
							result.put("bargainDetail",bargainDetail);
							result.put("msg","恭喜您砍价成功!您已经帮好友省了￥"+amount.setScale(2,BigDecimal.ROUND_HALF_UP)+"。感谢您的热心参与!");
						}
					}
				}
			}
			
		}else{//token无效,告知用户砍价失败,并通知砍价已过期或者无效的砍价地址
			result.put("isSuccess", false);//砍价成功
			result.put("code","404");
			result.put("msg","对不起砍价失败,砍价活动已过期或者无效的砍价地址,请核实!");
		}
	}
	
	/**
	 * 删除砍价明细
	 * @param bargainDetail
	 * @param result
	 * @return boolean
	 */
	@Override
	public void delete(BargainDetailBean bargainDetail, Map<String, Object> result) {
		if(bargainDetail.getBargainDetailId()!=null){
			Short status=-1;//删除
			bargainDetail.setStatus(status);
			int num=bargainDetailMapper.updateByPrimaryKeySelective(bargainDetail);
			result.put("bargainDetail",bargainDetail);
			if(num>0){//删除成功
				result.put("isSuccess", true);
				result.put("code", "000");
			}else{//删除失败
				result.put("isSuccess", false);
				result.put("code", "500");
			}
		}else{
			result.put("isSuccess", false);
			result.put("code", "404");
		}
		
	}

	
}
